<?php

namespace app\home\model;

use app\admin\validate\admin\Admin as val_Admin;
use app\admin\validate\merchant\Merchant;
use think\exception\ValidateException;
use think\facade\Session;
use think\Model;
use ulit\CryptAES;

class MerchantModel extends Model
{
    protected  $autoWriteTimestamp = 'datetime';

    protected $createTime = 'create_time';

    protected $json=['ip'];


    protected $jsonAssoc=true;
    protected $updateTime = 'update_time';

    protected $table = 'pay_merchant';

    public function getIpAttr($value)
    {

        return    $value ?implode(',',$value):$value;


    }

    public static function Login($params)
    {



            $where['name'] = $params['username'];

            $aes = new CryptAES();
            $password = $aes->aesEncode($params['password']);
            $where['password'] = $password;
            $result = self::where($where)->hidden(['password'])->find();
            if (empty($result)) {
                return [
                    'code' => 4002,
                    'msg' => '账号密码错误',
                ];
            } else {


                if (!empty($result->google) && $result->google != '0') {
                    $res = validate_google_code($result->google, $params['google']);
                    if (!$res) {
                        $msg = [
                            'code' => 4003,
                            'msg' => '谷歌验证码错误',
                        ];
                        return $msg;
                    }
                }
                $save['loginip'] = request()->ip();
                self::where($where)->update($save);
                session('member_id', $result->id);
                session('member_name', $result['name']);
                $msg = [
                    'code' => 200,
                    'msg' => '登录成功',
                    'url' => url('Index/index')->build(),
                ];
                return $msg;
            }

        }

    public static function edit($id){
        try {
            $add=input('');
            if (!empty($add['ip'])) {
                 $add['ip']=explode(',', $add['ip']);
            }
            $result = validate(Merchant::class)->scene('MemberModify')->check($add);
            if(!empty($result)){
                $result=self::where(['id'=>$id])->save($add);
                if($result){
                    $data = array(
                        'code' => 200,
                        'msg' => '成功!',
                    );

                    return $data;
                }else{
                    $data = array(
                        'code' => 40002,
                        'msg' => '失败!',
                    );
                    return $data;
                }
                // dump($result);die;
            }
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            return $e->getError();
        }
    }

    public static function merchantList(){

        $result=self::where(['id'=>\session('member_id')])->select();

        return $result;

    }

}